package com.ship.contract.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ship.common.core.base.BaseController;
import com.ship.common.core.base.Page;
import com.ship.common.core.web.domain.JsonResult;
import com.ship.common.log.annotation.Log;
import com.ship.common.log.enums.BusinessType;
import com.ship.contract.bean.*;
import com.ship.contract.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/excellentCases")
public class ConExcellentCasesController extends BaseController {

    @Autowired
    private ConExcellentCasesService conExcellentCasesService;
    @Autowired
    private ConResourceMainService conResourceMainService;
    @Autowired
    private ConResourceMemorandumService conResourceMemorandumService;
    @Autowired
    private ConShippingService conShippingService;
    @Autowired
    private ConShippingAgencyService conShippingAgencyService;
    @Autowired
    private ConThirdPartyMeasurementService conThirdPartyMeasurementService;
    @Autowired
    private ConInsuranceGoodsService conInsuranceGoodsService;
    @Autowired
    private ConRefuelingService conRefuelingService;
    @Autowired
    private ConLawyerService conLawyerService;
    @Autowired
    private ConInsurancePeopleService conInsurancePeopleService;
    @Autowired
    private ConBrokerService conBrokerService;

    /**
     * 分页
     *
     * @param conExcellentCases
     * @return
     */
    @PostMapping("/page")
    public JsonResult<Page<ConExcellentCases>> page(@RequestBody ConExcellentCases conExcellentCases) {
        conExcellentCases.setStatus(1);
        Page<ConExcellentCases> page = conExcellentCasesService.page(conExcellentCases,new QueryWrapper<ConExcellentCases>().eq("status",1));
        List<ConExcellentCases> exportList = page.getList();

        // 对列表按时间倒序排序
        Collections.sort(exportList, (e1, e2) -> e2.getCreateDate().compareTo(e1.getCreateDate()));

        long pageSize = page.getPageSize();
        long currentPage = page.getPageNum();
        int startOrderNumber = (int) ((currentPage - 1) * pageSize + 1);

        // 为每个Export对象设置orderNumber
        for (int i = 0; i < exportList.size(); i++) {
            exportList.get(i).setOrderNumber(startOrderNumber + i);
        }

        page.setList(exportList);
        return JsonResult.success(page);
    }

    /**
     * 删除申请
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @Log(title = "删除合同优秀案例",businessType = BusinessType.DELETE)
    public JsonResult delete(@PathVariable Long id){
        ConExcellentCases conExcellentCases = conExcellentCasesService.getById(id);
        if(null != conExcellentCases){
            Boolean judge = conExcellentCasesService.removeById(id);
            if(judge){
                if(null != conExcellentCases.getTypeOfContract()){
                    if(null != conExcellentCases.getConId()){
                        //资源合同-MA主合同
                        if(1==conExcellentCases.getTypeOfContract()){
                            ConResourceMain conResourceMain = conResourceMainService.getById(conExcellentCases.getConId());
                            if(null != conResourceMain){
                                conResourceMain.setExcellent(0);
                                conResourceMainService.updateById(conResourceMain);
                            }
                            //船运合同
                        }else if(2==conExcellentCases.getTypeOfContract()){
                            ConShipping conShipping = conShippingService.getById(conExcellentCases.getConId());
                            if(null != conShipping){
                                conShipping.setExcellent(0);
                                conShippingService.updateById(conShipping);
                            }
                            //船舶代理合同
                        }else if(3==conExcellentCases.getTypeOfContract()){
                            ConShippingAgency conShippingAgency = conShippingAgencyService.getById(conExcellentCases.getConId());
                            if(null != conShippingAgency){
                                conShippingAgency.setExcellent(0);
                                conShippingAgencyService.updateById(conShippingAgency);
                            }
                            //第三方计量合同
                        }else if(4==conExcellentCases.getTypeOfContract()){
                            ConThirdPartyMeasurement conThirdPartyMeasurement = conThirdPartyMeasurementService.getById(conExcellentCases.getConId());
                            if(null != conThirdPartyMeasurement){
                                conThirdPartyMeasurement.setExcellent(0);
                                conThirdPartyMeasurementService.updateById(conThirdPartyMeasurement);
                            }
                            //保险合同-货险
                        }else if(5==conExcellentCases.getTypeOfContract()){
                            ConInsuranceGoods conInsuranceGoods = conInsuranceGoodsService.getById(conExcellentCases.getConId());
                            if(null != conInsuranceGoods){
                                conInsuranceGoods.setExcellent(0);
                                conInsuranceGoodsService.updateById(conInsuranceGoods);
                            }
                            //加油合同
                        }else if(6==conExcellentCases.getTypeOfContract()){
                            ConRefueling conRefueling = conRefuelingService.getById(conExcellentCases.getConId());
                            if(null != conRefueling){
                                conRefueling.setExcellent(0);
                                conRefuelingService.updateById(conRefueling);
                            }
                            //律师合同
                        }else if(7==conExcellentCases.getTypeOfContract()){
                            ConLawyer conLawyer = conLawyerService.getById(conExcellentCases.getConId());
                            if(null != conLawyer){
                                conLawyer.setExcellent(0);
                                conLawyerService.updateById(conLawyer);
                            }
                            //资源合同-CN 备忘录
                        }else if(8==conExcellentCases.getTypeOfContract()){
                            ConResourceMemorandum conResourceMemorandum = conResourceMemorandumService.getById(conExcellentCases.getConId());
                            if(null != conResourceMemorandum){
                                conResourceMemorandum.setExcellent(0);
                                conResourceMemorandumService.updateById(conResourceMemorandum);
                            }
                            //保险合同-租船人责任险
                        }else if(9==conExcellentCases.getTypeOfContract()){
                            ConInsurancePeople conInsurancePeople = conInsurancePeopleService.getById(conExcellentCases.getConId());
                            if(null != conInsurancePeople){
                                conInsurancePeople.setExcellent(0);
                                conInsurancePeopleService.updateById(conInsurancePeople);
                            }
                            //船舶经纪人合同
                        }else if(10==conExcellentCases.getTypeOfContract()){
                            ConBroker conBroker = conBrokerService.getById(conExcellentCases.getConId());
                            if(null != conBroker){
                                conBroker.setExcellent(0);
                                conBrokerService.updateById(conBroker);
                            }
                        }
                    }
                }
            }
        }
        return JsonResult.success();
    }

    /**
     * 新增
     *
     * @param conExcellentCases
     * @return
     */
    @PostMapping
    @Log(title = "新增合同优秀案例",businessType = BusinessType.INSERT)
    public JsonResult<Object> create(@RequestBody ConExcellentCases conExcellentCases) {
        return conExcellentCasesService.saveCon(conExcellentCases);
    }
}
